package com.rabbitmq.ChattingRoom.controller;

import com.rabbitmq.ChattingRoom.controller.biz.UserBiz;
import com.rabbitmq.ChattingRoom.util.common.base.Result;
import com.rabbitmq.ChattingRoom.util.common.req.UpdateUserReq;
import com.rabbitmq.ChattingRoom.util.common.req.UserLoginReq;
import com.rabbitmq.ChattingRoom.util.common.req.UserRegisterReq;
import com.rabbitmq.ChattingRoom.util.common.resp.UserLoginRESP;
import com.rabbitmq.ChattingRoom.util.common.resp.UserRESP;
import io.swagger.annotations.ApiOperation;
import com.rabbitmq.ChattingRoom.util.common.req.UserLogoffReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping(value = "user",produces = "application/json;charts=utf-8")
public class UserController {

    @Autowired
    private UserBiz biz;

    /**
     * 用户注册
     */
    @ApiOperation(value = "用户注册", notes = "用户注册接口")
    @PostMapping(value = "register")
    public Result<String> Register(@RequestBody @Valid UserRegisterReq req){
        return biz.Register(req);
    }

    /**
     * 用户登录
     * 生成并存储token数据
     */
    @ApiOperation(value = "用户登录",notes = "用户登录接口")
    @PostMapping(value = "login")
    public Result<UserLoginRESP> login(@RequestBody @Valid UserLoginReq req) {
        return biz.login(req);
    }


    /**
     * 用户注销
     * 清除redis token数据
     */
    @ApiOperation(value = "用户注销", notes = "用户注销操作")
    @PostMapping(value = "logoff")
    public Result<String> logoff(@RequestBody @Valid UserLogoffReq req){
        return biz.logoff(req);
    }

    /**
     * 心跳检测
     * 更新token数据
     */
    @ApiOperation(value = "心跳检测", notes = "心跳检测接口，当前用户信息存储时间有效期为 3 分钟")
    @PostMapping(value = "keepAlive")
    public Result<String> keepAlive(@RequestHeader("TOKEN") String token) {
        return biz.keepAlive(token);
    }


    /**
     * 获取在线用户列表
     */
    @ApiOperation(value = "获取在线用户列表", notes = "获取在线用户列表(当前最大值为500)")
    @PostMapping(value = "getUserAliveCount")
    public Result<List<UserRESP>> getUserAliveCount(@RequestHeader("TOKEN") String token) {
        return biz.getUserAliveCount(token);
    }

    /**
     * 获取所有用户列表
     */
    @ApiOperation(value = "获取所有用户列表",notes = "获取所有用户列表")
    @PostMapping(value = "getUsers")
    public Result<List<UserRESP>> getUsers() {
        return biz.getUsers();
    }

    /**
     * 获取当前用户基本信息
     */
    @ApiOperation(value = "获取当前用户基本信息", notes = "获取当前用户基本信息")
    @PostMapping(value = "getUserInfo")
    public Result<UserRESP> getUserInfo(@RequestHeader("TOKEN") String token) {
        return biz.getUserInfo(token);
    }

    /**
     * 更新用户基本信息
     */
    @ApiOperation(value = "更新用户基本信息",notes = "更新用户基本信息")
    @PostMapping(value = "update")
    public Result<String> updatePwd(@RequestBody UpdateUserReq req) {
        return biz.updatePwd(req);
    }

}